package cn.riverluoo.parse;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import cn.riverluoo.console.ConsoleTextArea;

public class DomParse {

	public void parse(String newname) {
		ConsoleTextArea ct=new ConsoleTextArea();
		ct.showConsole();
		DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder docb = docbf.newDocumentBuilder();
			Document doc = docb.parse(new File(newname));
			// 获得当前节点的所有子节点
			NodeList nodes = doc.getChildNodes();
			showDom(nodes);
			System.out.println("**********************************************************************************");
			ReadTreeStructure(nodes);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void ReadTreeStructure(NodeList nodes) {
		// 遍历所有子节点
		for (int i = 0; i < nodes.getLength(); i++) {
			// 获得字节点名，判断子节点的类型，区分出text类型的node以及element类型的node
			if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {

				String value = ((Text) (nodes.item(i).getFirstChild()))
						.getData().trim();
				if (value.getBytes().length != 0) {
					System.out.println("		" + nodes.item(i).getNodeName() + ":"+ value);

				}else{
					Node my_node = nodes.item(i);
					if(my_node.hasAttributes()){
						Element element=(Element) nodes.item(i);
						NamedNodeMap attr=element.getAttributes();
						for (int j = 0; j < attr.getLength(); j++) {
							Node node = attr.item(j);
							System.out.println("	  "+nodes.item(i).getNodeName()+" "+node.getNodeName()+" = "+node.getNodeValue());
						}
					}else{
						System.out.println("	"+nodes.item(i).getNodeName());
					}
				}
			}
			if (nodes.item(i).getChildNodes().getLength() != 0) {
				ReadTreeStructure(nodes.item(i).getChildNodes());
			}
		}
	}

	public static void showDom(NodeList nodes) {
		// 遍历所有子节点
		for (int i = 0; i < nodes.getLength(); i++) {
			// 获得字节点名，判断子节点的类型，区分出text类型的node以及element类型的node
			if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
				String value = ((Text) (nodes.item(i).getFirstChild()))
						.getData().trim();
				if (value.getBytes().length != 0) {
					System.out.println("		"+"ELEMENT: " + nodes.item(i).getNodeName());
					System.out.println("		    "+"TEXT: " +  value);
				}else{
					Node my_node = nodes.item(i);
					if(my_node.hasAttributes()){
						System.out.println("	  "+"ELEMENT: "+nodes.item(i).getNodeName());
						Element element=(Element) nodes.item(i);
						NamedNodeMap attr=element.getAttributes();
						for (int j = 0; j < attr.getLength(); j++) {
							Node node = attr.item(j);
							System.out.println("            "+"ATTRIBUTE: "+node.getNodeName()+" - "+node.getNodeValue());
							System.out.println("                 "+"TEXT: "+"#text"+" = "+node.getNodeValue());
						}
					}else{
						System.out.println("	  "+"ELEMENT: "+nodes.item(i).getNodeName()+" - "+null);
					}
				}
			}
			// 如果子节点还有子节点就继续往下层读
			if (nodes.item(i).getChildNodes().getLength() != 0) {
				showDom(nodes.item(i).getChildNodes());
			}
		}
	}



}
